// columns.ts

import DictTag, { DictValueEnum } from "@/components/DictTag";
import { createIcon } from "@/utils/IconUtil";
import { ProColumns } from "@ant-design/pro-table";
import { Avatar, Button, Divider, Dropdown, MenuProps, Modal, Popconfirm, Space, Switch, Typography } from "antd";
import dayjs from "dayjs";
// 定义所有列的工厂函数
export const createTableColumns = (
	handleColumnRemove: (record: API.Setting.OnlineUserType) => Promise<any>
): ProColumns<API.Setting.OnlineUserType>[] => {
	return [
		{
			title: "会话令牌",
			dataIndex: "tokenId",
			valueType: "text",
			hideInSearch: true,
			ellipsis: true
		},
		{
			title: "用户账号",
			dataIndex: "userName",
			valueType: "text"
		},
		{
			title: "部门名称",
			dataIndex: "deptName",
			valueType: "text",
			hideInSearch: true
		},
		{
			title: "IP地址",
			dataIndex: "ipaddr",
			valueType: "text"
		},
		{
			title: "登录地点",
			dataIndex: "loginLocation",
			valueType: "text",
			hideInSearch: true,
			ellipsis: true
		},
		{
			title: "客户端类型",
			dataIndex: "browser",
			valueType: "text",
			hideInSearch: true
		},
		{
			title: "操作系统",
			dataIndex: "os",
			valueType: "text",
			hideInSearch: true,
			ellipsis: true
		},
		{
			title: "登录时间",
			dataIndex: "loginTime",
			valueType: "dateRange",
			width: 200,
			render: (_: any, record: any) => {
				return <span>{dayjs(record?.loginTime).format("YYYY-MM-DD HH:mm:ss")}</span>;
			},
			hideInSearch: true,
			search: {
				transform: (value: any) => {
					return {
						"params[beginTime]": value[0],
						"params[endTime]": value[1]
					};
				}
			}
		},
		{
			title: "操作",
			dataIndex: "option",
			width: "220px",
			valueType: "option",
			render: (_, record: any) => {
				return (
					<Button
						type="link"
						size="small"
						danger
						key="batchRemove"
						onClick={async () => {
							Modal.confirm({
								title: "强制下线",
								content: `确定强制把用户账号为：${record.userName}踢下线吗？`,
								okText: "确认",
								cancelText: "取消",
								onOk: async () => {
									return new Promise<void>((resolve, reject) => {
										return handleColumnRemove(record)
											.then(() => {
												resolve();
											})
											.catch(() => {
												reject();
											});
									});
								}
							});
						}}
					>
						强退
					</Button>
				);
			}
		}
	];
};

export const proTableCardProps = {
	bodyStyle: {
		paddingInline: 0,
		paddingBlock: 0
	}
};

export const pagination = {
	showQuickJumper: true,
	showSizeChanger: true
};
export const tableTitle = "在线用户";

export const formTitle = ["添加用户", "编辑用户"];
